var req;
var globalHasFile;

function sendMail()
{
    var hasFile = "0";
    var x = document.getElementsByTagName('input');
    for (var i=0;i<x.length;i++)
    {
        if (x[i].type == 'file')
        {
            if(x[i].value != "")
            {
                hasFile = "1";
                break;
            }
        }
    }
    globalHasFile = hasFile;
//    alert(hasFile);

    updateRTEs();

    var to = document.RTEDemo.txtTo.value;
    var cc = document.RTEDemo.txtCc.value;
    var bcc = document.RTEDemo.txtBcc.value;
    var subject = document.RTEDemo.txtSubject.value;
    var rte1 = escape(document.RTEDemo.rte1.value);

    var url = "/SRS-war/SendMailServlet?to=" + to + "&cc=" + cc + "&bcc=" + bcc +
        "&subject=" + subject + "&rte1=" + rte1 + "&hasFile=" + hasFile;

//    alert (document.RTEDemo.file1.value);
//    document.RTEDemo.submit();

    if (window.XMLHttpRequest)
    {
        req = new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }

    req.open("Get",url,true);
    req.onreadystatechange = callback;
    req.send(null);
}

function callback()
{
    if (req.readyState==4)
    {
        if (req.status == 200)
        {
//            alert(req.responseText);
            if(req.responseText == "ok")
            {
                if(globalHasFile == "1")
                {
                    document.RTEDemo.submit();
                }
                else
                {
                    window.location = "sendMailSuccess.jsp";
                }

            }
        }
    }
}

//function test()
//{
//    updateRTEs();
//
//    var rte1 = document.RTEDemo.rte1.value;
//    var to = document.RTEDemo.txtTo.value;
//
//    alert (rte1 + "\n" + to);
//
//    document.RTEDemo.submit();
//}

